package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.TianJieFinanceGame;
import org.itboys.mobile.service.mongo.web.TianJieGameService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：圣代_支付_参数_controller_web
 */
@Controller
@RequestMapping("/tianjie/web/finance/game")
public class TianJieGameController extends BaseController{
    @Autowired
    private TianJieGameService gameService;

    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        Model model){
            return "/tianjie/game/index";
    }


    /**
     * 查询积分配置
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            PageResult<TianJieFinanceGame> result=gameService.selectGame(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 新增或者修改积分配置
     * @param request
     * @param response
     * @param game
     */
    @RequestMapping("/save")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute TianJieFinanceGame game,
            @RequestParam(value="imageFile",required=false) MultipartFile imageFile){
        try {
            gameService.saveGame(game,imageFile);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/game/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }

    /**
     * 单个查询
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/findOne")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="id",required = true)Long id){
        try {
            TianJieFinanceGame game=gameService.findOne(id);
            ResponseMessage.success(game);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/game/findOne------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);

        }
    }
    /**
     * 删除
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="id",required = true)Long id){
        try {
            gameService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/tianjie/web/finance/game/delete------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
    }




}
